package com.yegs.jindie.util;

import com.yegs.jindie.entity.DeliveryTO;
import com.yegs.jindie.entity.InventoryTO;
import com.yegs.jindie.entity.PurchaseTO;
import com.yegs.jindie.entity.ToFactory;

import java.io.*;
import java.util.List;

public class TxtUtil {

    private boolean checkBeing(String path){
        File file = new File(path);
        return file.exists();
    }

    private String readText(String path) throws IOException {
        File file = new File(path);
        Reader reader = new FileReader(file);
        char[] ch = new char[5];
        StringBuilder sb = new StringBuilder();
        int len = -1;
        while (-1 != (len = reader.read(ch))){
            sb.append(new String(ch,0,len));
        }
        reader.close();
        return sb.toString();
    }
    private void writeText(String path,String append) throws IOException {
        File file = new File(path);
        Writer writer = new FileWriter(file);
        writer.write(append);
        writer.append(append);
        writer.flush();
        writer.close();
    }

    public static String exporDeliveryTOTxt(String path,List<DeliveryTO> list) throws IOException {
        if(!path.contains(".txt")){
            path = path + ".txt";
        }
        File file = new File(path);
        Writer writer = new FileWriter(file);
        try {
            for(DeliveryTO dl : list){
                writer.append(ToFactory.getDeliveryTxt(dl));
            }
            writer.close();
        }catch (Exception e){
            writer.close();
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
        return path;
    }
    public static String exporPurchaseTOTxt(String path,List<PurchaseTO> list) throws IOException {
        if(!path.contains(".txt")){
            path = path + ".txt";
        }
        File file = new File(path);
        Writer writer = new FileWriter(file);
        try {
            for(PurchaseTO pur : list){
                writer.append(ToFactory.getPurchaseTOTxt(pur));
            }
            writer.close();
        }catch (Exception e){
            writer.close();
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
        return path;
    }
    public static String exporInventoryTOTxt(String path,List<InventoryTO> list) throws IOException {
        if(!path.contains(".txt")){
            path = path + ".txt";
        }
        File file = new File(path);
        Writer writer = new FileWriter(file);
        try {
            for(InventoryTO inventoryTO : list){
                writer.append(ToFactory.getInventoryTOTxt(inventoryTO));
            }
            writer.close();
        }catch (Exception e){
            writer.close();
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
        return path;
    }




    public static void main(String args[]) throws IOException {
        String path = "D:\\jindie\\8143_CN_INV_2020120399.txt";
        TxtUtil textUtil = new TxtUtil();
        textUtil.writeText(path,"hello world");
        textUtil.writeText(path,"\n");
        textUtil.writeText(path,"hello  world");
        String s = textUtil.readText(path);
        System.out.println(s);
        System.out.println(s.length());
    }



}
